package com.nwpu.coursesupport.service.imple;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.CourseSearchCriteria;
import com.nwpu.coursesupport.entity.Course;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.mapper.CourseMapper;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.CourseServiceParam;
import com.nwpu.coursesupport.service.param.IDListParam;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.UpdateUtil;
import com.nwpu.coursesupport.view.CourseView;


@Service
public class CourseService extends AbstractService{
    @Autowired
    private CourseMapper courseMapper;

	protected static Logger logger = LoggerFactory.getLogger(CourseService.class);

    
    public JSONResult detail(IDParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        Course course = this.courseMapper.findById(p.getId());
        if(course==null){
        	logger.error("该课程不存在");
        	return rst.fail("该课程不存在");
        }
        rst.addData("course", course);
        rst.success();
        return rst;
    }

    public JSONResult listView(CourseSearchCriteria sc){
        JSONResult rst = new JSONResult();
        rst.fail();
        if (sc == null||sc.getTid()<=0){
        	RoleType roleType=(RoleType) getSession().getAttribute("roleType");
        	if(roleType!=RoleType.TEACHER){
        		logger.error("参数有误");
        		return rst.fail("参数有误");
        	}
            sc = new CourseSearchCriteria();
            Integer tid =(Integer) getSession().getAttribute("id");
            sc.setTid(tid);
        }
        
        List<CourseView> list = this.courseMapper.findView(sc);
        if (list == null){
        	logger.error("数据查询失败");
            return rst.fail("数据查询失败");
        }
        
        rst.addData("list", list);
        rst.success();
        return rst;
    }
    
    public JSONResult list(CourseServiceParam.SearchParam p){
        JSONResult rst = new JSONResult();
        rst.fail();
       
        if (p == null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        
        if (p.getPageNo() <= 0){
			p.setPageNo(1);
		}
        CourseSearchCriteria sc = p.getSearch();
        RoleType roleType=(RoleType) getSession().getAttribute("roleType");
        if (sc == null||sc.getTid()<=0){
        	if(roleType!=RoleType.TEACHER){
        		logger.error("参数有误");
        		return rst.fail("参数有误");
        	}
            sc = new CourseSearchCriteria();
            Integer tid =(Integer) getSession().getAttribute("id");
            sc.setTid(tid);
        }
        int pageSize = 6;
        int totalCount =this.courseMapper.count(sc);
        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (p.getPageNo() > pageCount){
			p.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
        page.setNumber(p.getPageNo());
        page.setSize(pageSize);
        
        List<Course> list = this.courseMapper.find(sc,page);
        if (list == null){
        	logger.error("数据查询失败");
            return rst.fail("数据查询失败");
        }
        
        rst.addData("list", list);
        rst.addData("pageNo", p.getPageNo());
        rst.addData("allpages", pageCount);
        rst.success();
        return rst;
    }

    public JSONResult listData(CourseServiceParam.SearchParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
       
        if (p == null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        if (p.getPageNo() <= 0){
			p.setPageNo(1);
		}
        CourseSearchCriteria sc = p.getSearch();
        RoleType roleType=(RoleType) getSession().getAttribute("roleType");
        if (sc == null||sc.getTid()<=0){
        	if(roleType!=RoleType.TEACHER){
        		logger.error("参数有误");
        		return rst.fail("参数有误");
        	}
            sc = new CourseSearchCriteria();
            Integer tid =(Integer) getSession().getAttribute("id");
            sc.setTid(tid);
        }
        int pageSize = 6;
        int totalCount =this.courseMapper.count(sc);
        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (p.getPageNo() > pageCount){
			p.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
        page.setNumber(p.getPageNo());
        page.setSize(pageSize);
        
        List<Course> list = this.courseMapper.find(sc, page);
        if (list == null){
        	logger.error("数据查询失败");
            return rst.fail("数据查询失败");
        }
        
        rst.addData("list", list);
        rst.addData("page", p.getPageNo());
        rst.addData("allpages", pageCount);
        rst.success();
        return rst;
    }

    public JSONResult create(CourseServiceParam.CreateParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null){
        	logger.error("数据查询失败");
            return rst.fail("参数有误");
        }
        
        Course course = p;
        Date date=new Date();
        course.setCreateDate(date);
        course.setUpdateDate(date);
        Integer tid=(Integer)getSession().getAttribute("id");
        String tname=(String)getSession().getAttribute("name");
        course.setTname(tname);
        course.setTid(tid);
        course.setUpdateId(tid);
        if (!this.courseMapper.save(course)){
        	logger.error("保存失败");
            return rst.fail("保存失败");
        }
        
        rst.addData("course", course);
        rst.success();
        return rst;
    }

    public JSONResult update(CourseServiceParam.UpdateParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        Course course = this.courseMapper.findById(p.getId());
        if (course == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }
        
        int id=(int) getSession().getAttribute("id");
        if(id!=p.getTid()){
        	logger.error("你没有修改该课程的权限");
        	return rst.role("你没有修改该课程的权限");
        }
        
        if (UpdateUtil.setValues(course, p) > 0){
        	Date date=new Date();
        	course.setUpdateDate(date);
        	course.setUpdateId((Integer)getSession().getAttribute("id"));
            if (!this.courseMapper.update(course)){
            	logger.error("保存失败");
                return rst.fail("保存失败");
            }
        }
        
        rst.addData("course", course);
        rst.success();
        return rst;
    }

    public JSONResult remove(IDParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }

        
        Course course = this.courseMapper.findById(p.getId());
        if (course == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }

        if (!this.courseMapper.delete(p.getId())){
        	logger.error("删除失败");
            return rst.fail("删除失败");
        }
        rst.addData("course", course);
        rst.success();
        return rst;
    }

    public JSONResult removeAll(IDListParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getIds() == null || p.getIds().isEmpty()){
            rst.fail("参数有误");
            return rst;
        }
        
        this.courseMapper.batchDelete(p.getIds());

        rst.success();
        return rst;
    }
}